package com.analyse.stock.strategy;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.analyse.stock.annotation.StrategyType;
import com.analyse.stock.component.TradingBizAbstract;
import com.analyse.stock.enums.StrategyEnum;
import com.analyse.stock.form.PickStockQueryForm;
import com.analyse.stock.form.TradingForm;
import com.analyse.stock.model.*;
import com.analyse.stock.service.IStockExchange;
import com.analyse.stock.service.IStockInfo;
import com.analyse.stock.service.IStrategyPoolService;
import com.analyse.stock.utils.PublicHolidayUtil;
import com.analyse.stock.utils.StockStringUtil;
import com.analyse.stock.vo.StrategyAnalyseResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.ta4j.core.BarSeries;
import org.ta4j.core.Rule;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 集合竞价策略
 **/
@Slf4j
@Component
@StrategyType(strategy = StrategyEnum.TOP_RANK_TRADING)
public class TopRankTradingIStrategy implements IStrategy {

    @Autowired
    private IStockInfo istockInfo;

    @Autowired
    private IStockExchange stockExchange;

    @Autowired
    private IStrategyPoolService strategyPoolService;

    @Override
    public StrategyAnalyseResultVO run(String paramJson)  {
        PickStockQueryForm queryForm = JSONObject.parseObject(paramJson, PickStockQueryForm.class);
        StockInfoModel stockModel = istockInfo.getStockInfo(queryForm.getStockCode());
        if(Objects.isNull(stockModel)){
            return null;
        }
        //读取股票所属的交易所
        StockExchangeModel exchangeModel =
                stockExchange.getStockExchange(stockModel.getCountryCode(),stockModel.getExchangeCode());
        if(Objects.isNull(exchangeModel)){
            return null;
        }
        boolean isTradingDate = PublicHolidayUtil.isTradingDate(stockModel.getExchangeCode(), DateUtil.date());
        if(!isTradingDate){
            return null;
        }
        StrategyPoolModel strategyPool = strategyPoolService.getStrategyPoolByCode(StrategyEnum.T1_TRADING_STRATEGY.getCode());

        String userId = "admin";
        String accountId = "admin";
        TradingForm tradingForm = TradingForm.me(userId,accountId,stockModel,strategyPool);
        new TradingBizAbstract<StrategyAnalyseResultVO>(tradingForm){
            @Override
            public boolean verify(Date now) {
                return PublicHolidayUtil.isTradingTime(now,
                        StockStringUtil.getTradingTimeFun(exchangeModel.getLocalOpen(),exchangeModel.getLocalClose()));
            }
            @Override
            public boolean isTrading(Date now) {
                return PublicHolidayUtil.isTradingTime(now, StockStringUtil.topRankTradingTimeFun);
            }

            @Override
            public UserPositionInfoModel getUserPositionData(String userId,String accountId,String stockCode) {
                return null;
            }

            @Override
            public List<StockKlineDailyModel> getKlineData(PickStockQueryForm conditionForm) {
                return null;
            }

            @Override
            public StockCoreInfoModel pullRealTimeData(String stockCode) {
                return null;
            }

            @Override
            public StrategyAnalyseResultVO execSellRule(BarSeries barSeries,UserPositionInfoModel positionData,
                                                        List<Rule> sellRule, StockCoreInfoModel realTimeData) {
                return null;
            }

            @Override
            public StrategyAnalyseResultVO execBuyRule(BarSeries barSeries,UserPositionInfoModel positionData,
                                                       List<Rule> buyRule, StockCoreInfoModel realTimeData) {
                return null;
            }

        }.exec();
        return null;
    }
}
